home *** CD-ROM | disk | FTP | other *** search
/ Collection of Internet / Collection of Internet.iso / msdos / lynx / source / www / library / implemen / htaafile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-25  |  5.6 KB  |  206 lines

  1.  
  2. /* MODULE                            HTAAFile.c
  3. **        FILE ROUTINES FOR AUTHENTICATION
  4. **        (PASSWD AND GROUP FILES) AND
  5. **        ACCESS CONTROL LIST (.www_acl)
  6. ** AUTHORS:
  7. **    AL    Ari Luotonen    luotonen@dxcern.cern.ch
  8. **
  9. ** HISTORY:
  10. **
  11. **
  12. ** BUGS:
  13. **
  14. **
  15. */
  16.  
  17.  
  18. #include <stdio.h>        /* FILE */
  19. #include <string.h>
  20. #include "tcp.h"        /* Macro FROMASCII() */
  21. #include "HTAAUtil.h"        /* Common utilities used in AA */
  22. #include "HTAAFile.h"        /* Implemented here */
  23.  
  24.  
  25. #define SPACE            ' '
  26. #define TAB            '\t'
  27.  
  28.  
  29.  
  30. /* PUBLIC                        HTAAFile_nextRec()
  31. **        GO TO THE BEGINNING OF THE NEXT RECORD
  32. ** ON ENTRY:
  33. **    fp    is the file from which records are read from.
  34. **
  35. ** ON EXIT:
  36. **    returns    nothing. File read pointer is located at the beginning
  37. **        of the next record. Handles continuation lines
  38. **        (lines ending in comma indicate a following
  39. **        continuation line).
  40. **
  41. */
  42. PUBLIC void HTAAFile_nextRec ARGS1(FILE *, fp)
  43. {
  44.     int ch = getc(fp);
  45.     int last = (char)0;
  46.  
  47.     do {
  48.     while (ch != EOF  &&  ch != CR  &&  ch != LF) {
  49.         if (ch != ' '  && ch != '\t')
  50.         last = ch;        /* Last non-whitespace */
  51.         ch = getc(fp);        /* Skip until end-of-line */
  52.     }
  53.     while (ch != EOF &&
  54.            (ch == CR  ||  ch == LF))/*Skip carriage returns and linefeeds*/
  55.         ch = getc(fp);
  56.     if (ch != EOF)
  57.         ungetc(ch, fp);
  58.     } while (last == ',' && ch != EOF);    /* Skip also continuation lines */
  59. }
  60.  
  61.  
  62. /* PRIVATE                            read_item()
  63. **        READ AN ITEM FROM A PASSWORD, GROUP
  64. **        OR ACCESS CONTROL LIST FILE
  65. **        i.e. either a field, or a list item.
  66. ** ON ENTRY:
  67. **    fp        is the file to read the characters from
  68. **    contents    is the character array to put the characters
  69. **    reading_list    if TRUE, read a list item (ends either in
  70. **            acomma or acolon),
  71. **            if FALSE, read a field (ends in acolon).
  72. **    max_len        is the maximum number of characters that may
  73. **            be read (i.e. the size of dest minus one for
  74. **            terminating null).
  75. ** ON EXIT:
  76. **    returns        the terminating character
  77. **            (i.e. either separator or CR or LF or EOF).
  78. **    contents    contains a null-terminated string representing
  79. **            the read field.
  80. ** NOTE 1:
  81. **            Ignores leading and trailing blanks and tabs.
  82. ** NOTE 2:
  83. **            If the item is more than max_len characters
  84. **            long, the rest of the characters in that item
  85. **            are ignored.  However, contents is always
  86. **            null-terminated!
  87. */
  88. PRIVATE int read_item ARGS4(FILE *,    fp,
  89.                 char *,    contents,
  90.                 BOOL,    reading_list,
  91.                 int,    max_len)
  92. {
  93.     char * dest = contents;
  94.     char * end = contents;
  95.     int cnt = 0;
  96.     int ch = getc(fp);
  97.  
  98.     while (SPACE == ch || TAB == ch)    /* Skip spaces and tabs */
  99.     ch = getc(fp);
  100.  
  101.     while (ch != FIELD_SEPARATOR &&
  102.        (!reading_list || ch != LIST_SEPARATOR) &&
  103.        ch != CR  &&  ch != LF  &&  ch != EOF  &&  cnt < max_len) {
  104.     *(dest++) = ch;
  105.     cnt++;
  106.     if (ch != SPACE && ch != TAB)
  107.         end = dest;
  108.     ch = getc(fp);
  109.     } /* while not eol or eof or too many read */
  110.  
  111.     if (cnt == max_len)    {
  112.     /* If the field was too long (or exactly maximum) ignore the rest */
  113.     while (ch != FIELD_SEPARATOR &&
  114.            (!reading_list || ch != LIST_SEPARATOR) &&
  115.            ch != CR  &&  ch != LF  &&  ch != EOF)
  116.         ch = getc(fp);
  117.     }
  118.  
  119.     if (ch == CR || ch == LF)
  120.     ungetc(ch, fp);    /* Push back the record separator (NL or LF) */
  121.  
  122.     /* Terminate the string, truncating trailing whitespace off.
  123.     ** Otherwise (if whitespace would be included), here would
  124.     ** be *dest='\0'; and  cnt -= ... would be left out.
  125.     */
  126.     *end = '\0';
  127.     cnt -= dest-end;
  128.  
  129.     return ch;        /* Return the terminating character */
  130. }
  131.  
  132.  
  133.  
  134. /* PUBLIC                        HTAAFile_readField()
  135. **        READ A FIELD FROM A PASSWORD, GROUP
  136. **        OR ACCESS CONTROL LIST FILE
  137. **        i.e. an item terminated by colon,
  138. **        end-of-line, or end-of-file. 
  139. ** ON ENTRY:
  140. **    fp        is the file to read the characters from
  141. **    contents    is the character array to put the characters
  142. **    max_len        is the maximum number of characters that may
  143. **            be read (i.e. the size of dest minus one for
  144. **            terminating null).
  145. ** ON EXIT:
  146. **    returns        the terminating character
  147. **            (i.e. either separator or CR or LF or EOF).
  148. **    contents    contains a null-terminated string representing
  149. **            the read field.
  150. ** NOTE 1:
  151. **            Ignores leading and trailing blanks and tabs.
  152. ** NOTE 2:
  153. **            If the field is more than max_len characters
  154. **            long, the rest of the characters in that item
  155. **            are ignored.  However, contents is always
  156. **            null-terminated!
  157. */
  158. PUBLIC int HTAAFile_readField ARGS3(FILE *, fp,
  159.                     char *, contents,
  160.                     int,    max_len)
  161. {
  162.     return read_item(fp, contents, NO, max_len);
  163. }
  164.  
  165.  
  166.  
  167.  
  168. /* PUBLIC                        HTAAFile_readList()
  169. **
  170. **            READ A LIST OF STRINGS SEPARATED BY COMMAS
  171. **            (FROM A GROUP OR ACCESS CONTROL LIST FILE)
  172. ** ON ENTRY:
  173. **    fp        is a pointer to the input file.
  174. **    result        is the list to which append the read items.
  175. **    max_len        is the maximum number of characters in each
  176. **            list entry (extra characters are ignored).
  177. ** ON EXIT:
  178. **    returns        the number of items read.
  179. **
  180. */
  181. PUBLIC int HTAAFile_readList ARGS3(FILE *,    fp,
  182.                    HTList *,    result,
  183.                    int,        max_len)
  184. {
  185.     char *item = NULL;
  186.     char terminator;
  187.     int cnt = 0;
  188.  
  189.     do {
  190.     if (!item  &&  !(item = (char*)malloc(max_len+1)))
  191.         outofmem(__FILE__, "HTAAFile_readList");
  192.     terminator = read_item(fp, item, YES, max_len);
  193.     if (strlen(item) > 0) {
  194.         cnt++;
  195.         HTList_addObject(result, (void*)item);
  196.         item = NULL;
  197.     }
  198.     } while (terminator != FIELD_SEPARATOR  &&
  199.          terminator != CR  &&  terminator != LF  &&
  200.          terminator != EOF);
  201.  
  202.     if (item) free(item);    /* This was not needed */
  203.     return cnt;
  204. }
  205.  
  206.